<template>
  <div>
      <!-- 搜索表单 -->
    <el-form ref="searcForm" :inline="true" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="name" style="width: 200px">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item prop="code" style="width: 200px">
        <el-input v-model="searchMap.code" placeholder="商品编号"></el-input>
      </el-form-item>
      <el-form-item prop="supplierName" style="width: 200px">
        <el-input v-model="searchMap.supplierName" placeholder="选择供应商" readonly @click.native="dialogSupplierVisible=true"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="feachData">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('searcForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 商品列表 -->
    <el-table :data="list" height="380" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="code" label="商品编码" width="100"></el-table-column>
      <el-table-column prop="spec" label="商品规格"></el-table-column>
      <el-table-column prop="retailPrice" label="零售价"></el-table-column>
      <el-table-column prop="purchasePrice" label="进货价"></el-table-column>
      <el-table-column prop="storageNum" label="库存数量"></el-table-column>
      <el-table-column prop="supplierName" label="供应商"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 选择供应商对话框 -->
    <el-dialog title="选择供应商" :visible.sync="dialogSupplierVisible" width="500px">
        <supplier @option-supplier="optionSupplier" :isDialog="true"></supplier>
    </el-dialog>
    <el-dialog title="商品编辑" :visible.sync="dialogFormVisible" width="500px">
        <el-form :model="pojo" ref="pojoForm" label-width="100px" label-posion="right" style="width: 400px" :rules="rules">
            <el-form-item label="商品名称" prop="name">
                <el-input v-model="pojo.name"></el-input>
            </el-form-item>
            <el-form-item label="商品编码" prop="code">
                <el-input v-model="pojo.code"></el-input>
            </el-form-item>
            <el-form-item label="商品规格" prop="spec">
                <el-input v-model="pojo.spec"></el-input>
            </el-form-item>
            <el-form-item label="零售价" prop="retailPrice">
                <el-input v-model="pojo.retailPrice"></el-input>
            </el-form-item>
            <el-form-item label="进货价" prop="purchasePrice">
                <el-input v-model="pojo.purchasePrice"></el-input>
            </el-form-item>
            <el-form-item label="库存数量" prop="storageNum">
                <el-input v-model="pojo.storageNum"></el-input>
            </el-form-item>
            <el-form-item label="赞助商名称" prop="supplierName">
                <el-input v-model="pojo.supplierName" readonly @click.native="dialogSupplierVisible=true"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="!pojo.id ? addData('pojoForm') : update('pojoForm')">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import goodsApi from "@/api/goods";
import Supplier from "@/views/supplier"

export default {
    // 注册supplier作为子组件
  components: {Supplier},

  data() {
    return {
      list: [],
      pageSize: 10,
      currentPage: 1,
      total: 0,
      searchMap: {
        name: '',
        code: '',
        supplierName: ''
      },
      dialogSupplierVisible: false,
      dialogFormVisible: false,
      pojo:{
          id: null,
          name: '',
          code: '',
          spec: '',
          retailPrice: '',
          purchasePrice: '',
          storageNum: '',
          supplierName: '',
          isEdit: false
      },
      rules: {
          name: [
              {required: true, message: '商品名称不能为空', trigger: 'blur'}
          ],
          code: [
              {required: true, message: '商品编号不能为空', trigger: 'blur'}
          ],
          retailPrice: [
              {required: true, message: '零售价不能为空', trigger: 'blur'}
          ]
      }
    };
  },

  created() {
    this.feachData();
  },

  methods: {
    feachData() {
      goodsApi
        .search(this.currentPage, this.pageSize, this.searchMap)
        .then(response => {
          const resp = response.data;
          this.list = resp.data.rows;
          this.total = resp.data.total;
        });
    },
    // 分页
    handleSizeChange(val){
        this.pageSize = val
        this.feachData()
    },
    handleCurrentChange(val){
        this.currentPage = val
        this.feachData()
    },
    // 触发父子组件传递
    optionSupplier(currentRow){
        if(this.isEdit){
            this.pojo.supplierName = currentRow.name
            this.pojo.id = currentRow.id
        }else{
            this.searchMap.supplierName = currentRow.name
            this.searchMap.supplierId = currentRow.id
        }
        this.isEdit = false
        this.dialogSupplierVisible = false
    },
    // 添加商品
    handleAdd(){
        this.dialogFormVisible = true
        this.isEdit = true
        this.$nextTick(() => {
            this.$refs['pojoForm'].resetFields()
      })
    },
    // 确认添加商品
    addData(fromName){
        this.$refs[fromName].validate(valid => {
            if(valid){
                goodsApi.add(this.pojo).then(response => {
                    const resp = response.data
                    if(resp.flag){
                        console.log('添加成功')
                        this.dialogFormVisible = false
                    }else{
                        this.$message({
                            message: resp.message,
                            type: 'warning'
                        })
                    }
                })
            }else{
                return false
            }
        })
    },
    // 打开商品编辑窗口
    handleEdit(id){
        this.handleAdd()
        goodsApi.getById(id).then(response => {
            const resp = response.data
            this.pojo = resp.data
        })
    },
    // 确认修改商品信息
    update(fromName){
        this.$refs[fromName].validate(valid => {
            if(valid){
                goodsApi.update(this.pojo).then(response => {
                    const resp = response.data
                    if(resp.flag){
                        this.feachData()
                        this.dialogFormVisible = false
                    }else{
                        this.$message({
                        message: resp.message,
                        type: 'warning'
                        })
                    }
                    
                })
            }else{
                return false
            }
        })
    },
    // 删除商品信息
    handleDelete(id){
        this.$confirm('确认删除这条记录吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(() => {
        console.log('确认')
         goodsApi.deleteById(id).then(response => {
          const resp = response.data
          this.$message({
            message: resp.message,
            type: resp.flag ? 'success' : 'error'
          })
          if(resp.flag){
              this.feachData()
          }
        })
      }).catch(() => {
        console.log('取消')
      })
    },
    // 重置
    resetForm(formName) {
        this.$refs[formName].resetFields();
      },
  }
};
</script>

<style scoped>
.demo-form-inline {
  margin-top: 20px;
}
</style>